package guan.controller;

import guan.bean.ResCode;
import guan.bean.ResCodeType;
import guan.core.bean.Page;
import guan.dao.ResCodeTypeDao;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/resCodeType")
public class ResCodeTypeController {
	static Logger log = LoggerFactory.getLogger(ResCodeTypeController.class);

	@Autowired
	private ResCodeTypeDao resCodeTypeDao;

	@RequestMapping(value = "/query", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String query(@ModelAttribute Page page, ModelMap modelMap) {
		modelMap.put("test", "resCodeType-query 测试");

		page.setList(resCodeTypeDao.page(page.getPageNum(), page.getPageSize(),
				page.getSearch()));
		page.setCount(resCodeTypeDao.count(page.getSearch()));
		log.debug("query");
		return "resCodeType";
	}

	@RequestMapping(value = "/view")
	public String view(ModelMap modelMap) {
		modelMap.put("test", "resCodeType-view");
		log.debug("form");
		return "resCodeType";
	}

	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	public String save(ModelMap modelMap,
			@ModelAttribute ResCodeType resCodeType) {
		if(resCodeType.getResCodeList()!=null&&resCodeType.getResCodeList().size()>0){
			for(ResCode res:resCodeType.getResCodeList()){
				res.setResCodeType(resCodeType);
			}
		}
		//log.debug(ReflectionToStringBuilder.toString(resCodeType));
		resCodeTypeDao.saveOrUpdate(resCodeType);
		return "redirect:/resCodeType/query";
	}

	@RequestMapping(value = "/delete")
	public String delete(ModelMap modelMap) {
		modelMap.put("test", "resCodeType-delete");
		log.debug("delete");
		return "resCodeType";
	}

}
